﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

namespace KPIS.GERP.GFMS.APP.App_UI.UserControls
{
    public partial class DropDownList_BgmProject : System.Web.UI.UserControl
    {
        public RadComboBox Child;
        int nOffset = 10;
        Main main = new Main();
        IBLL_BGM_ProjectOperation pj = new B_BGM_ProjectOperation();

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        public RadComboBox GetComboBox()
        {
            RadComboBox ddl = ddlBgmProjectSeq;
            return ddl;
        }

        private void LoadData(RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            string strBgmSourceSeq = lblBgmSourceSeq.Text;
            string strBgmYear = lblBgmYear.Text;
            string strPersonEmpSeq = lblPersonEmpSeq.Text;

            IList<M_BGM_ProjectOperation> infolist = pj.GetProjectListByEmpSeq(strBgmYear, strBgmSourceSeq, strPersonEmpSeq);
            
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + nOffset, infolist.Count);
            e.EndOfItems = endOffset == infolist.Count;

            //ddlContactSeq.Items.Clear();

            for (int i = itemOffset; i < endOffset; i++)
            {
                ddlBgmProjectSeq.Items.Add(new RadComboBoxItem(infolist[i].bgm_proj_oper_name, infolist[i].bgm_proj_oper_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infolist.Count);
        }

        public void LoadControl(string strBgmSourceSeq, string strBgmYear, string strEmpSeq
            , Nullable<int> nWidth, Nullable<int> nSetOffset)
        {
            if (nWidth != null)
                ddlBgmProjectSeq.Width = Unit.Pixel(nWidth.Value);

            if (nSetOffset != null)
                nOffset = nSetOffset.Value;

            lblBgmSourceSeq.Text = strBgmSourceSeq;
            lblBgmYear.Text = strBgmYear;
            lblPersonEmpSeq.Text = strEmpSeq;
        }

        public void SetSelectedValue(string strValue)
        {
            if (strValue == null)
            {
                strValue = "";
            }

            if (strValue.Length > 0)
            {
                lblBgmProjectSeq.Text = strValue;

                if (ddlBgmProjectSeq.FindItemByValue(strValue) == null)
                {
                    M_BGM_ProjectOperation info = pj.GetInfo(strValue);
                    ddlBgmProjectSeq.SelectedValue = strValue;
                    ddlBgmProjectSeq.Text = info.bgm_proj_oper_name;
                }
                else
                {
                    ddlBgmProjectSeq.SelectedValue = strValue;
                }
            }
            else
            {
                ddlBgmProjectSeq.SelectedIndex = -1;
                ddlBgmProjectSeq.Text = "";
            }

            ddlBgmProjectSeq_SelectedIndexChanged(null, null);
        }

        public string GetSelectedValue()
        {
            string strValue = lblBgmProjectSeq.Text;

            return strValue;
        }

        public void ddlBgmProjectSeq_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            lblBgmProjectSeq.Text = ddlBgmProjectSeq.SelectedValue;

            switch (Page.ToString())
            {
                case "ASP.app_ui_forms_ess_moneyrequests_aspx":
                    Forms.ESS_MoneyRequests MoneyReq = (Forms.ESS_MoneyRequests)Page;
                    MoneyReq.LoadGrdFimMoneyReqDtls();
                    break;
            }

            if (Child != null)
            {
                Child.Text = "";

                if (ddlBgmProjectSeq.SelectedValue.Length > 0)
                {
                    switch (Child.ID)
                    {
                        case "ddlBgmProjectExpSeq":
                            App_UI.UserControls.DropDownList_BgmProjectExpense uc = (App_UI.UserControls.DropDownList_BgmProjectExpense)Child.Parent;
                            uc.LoadControl(ddlBgmProjectSeq.SelectedValue, lblBgmSourceSeq.Text, null, null);
                            break;
                    }
                }
                else
                {
                    Child.DataSource = null;
                    Child.DataBind();
                }
            }
        }

        protected void ddlBgmProjectSeq_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            LoadData(e);
        }
    }
}